package org.meichao.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@AutoConfigureAfter({MybatisConfig.class})
@EnableTransactionManagement
public class TransactionConfig extends DataSourceTransactionManagerAutoConfiguration {
    private Logger logger = LoggerFactory.getLogger(TransactionConfig.class);

    /**
     * 自定义事务mybatis自动参与到spring事务管理中，无需配置
     * 只要SqlSessionFactoryBean引用的数据源与DataSourceTransactionManager引用的数据源一致即可，否则事务不会起作用
     */
    @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSourceProxy") DataSourceProxy dataSourceProxy){
        logger.info("-----------transactionManager init----------");
        return new DataSourceTransactionManager(dataSourceProxy);

    }
}
